Concepts_of_Programming_Languages by Unknown
Author:Unknown
Language: eng
Format: epub
This section discusses the issues related to variables that are defined within sub-programs. The issue of nested subprogram definitions is also briefly covered.
9.4.1 Local Variables
Subprograms can define their own variables, thereby defining local referencing environments. Variables that are defined inside subprograms are called local variables , because their scope is usually the body of the subprogram in which they are defined.
400 Chapter 9 Subprograms
In the terminology of Chapter 5, local variables can be either static or stack dynamic. If local variables are stack dynamic, they are bound to storage when the subprogram begins execution and are unbound from storage when that execution terminates. There are several advantages of stack- dynamic local variables, the primary one being flexibility. It is essential that recursive subprograms have stack- dynamic local variables. Another advantage of stack- dynamic locals is that the storage for local variables in an active subprogram can be shared with the local variables in all inactive subprograms. This is not as important an advantage as it was when computers had smaller memories.
The main disadvantages of stack- dynamic local variables are the following: First, there is the cost of the time required to allocate, initialize (when neces-sary), and deallocate such variables for each call to the subprogram. Second, accesses to stack- dynamic local variables must be indirect, whereas accesses to static variables can be direct. 4 This indirectness is required because the place in the stack where a particular local variable will reside can be determined only during execution (see Chapter 10). Finally, when all local variables are stack dynamic, subprograms cannot be history sensitive; that is, they cannot retain data values of local variables between calls. It is sometimes convenient to be able to write history- sensitive subprograms. A common example of a need for a history- sensitive subprogram is one whose task is to generate pseudoran-dom numbers. Each call to such a subprogram computes one pseudorandom number, using the last one it computed. It must, therefore, store the last one in a static local variable. Coroutines and the subprograms used in iterator loop constructs (discussed in Chapter 8) are other examples of subprograms that need to be history sensitive.
The primary advantage of static local variables over stack- dynamic local variables is that they are slightly more efficient— they require no run- time over-head for allocation and deallocation. Also, if accessed directly, these accesses are obviously more efficient. And, of course, they allow subprograms to be history sensitive. The greatest disadvantage of static local variables is their inability to support recursion. Also, their storage cannot be shared with the local variables of other inactive subprograms.
In most contemporary languages, local variables in a subprogram are by default stack dynamic. In C and C++ functions, locals are stack dynamic unless specifically declared to be static . For example, in the following C (or C++) function, the variable sum is static and count is stack dynamic.
int adder( int list[], int listlen) {
static int sum = 0;
int count;
for (count = 0; count < listlen; count ++)
sum += list [count];
return sum;
9.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Mikado Method by Ola Ellnestam Daniel Brolund(21737)
Hello! Python by Anthony Briggs(20963)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(19448)
Dependency Injection in .NET by Mark Seemann(19010)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(18639)
Kotlin in Action by Dmitry Jemerov(18461)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(18224)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(17084)
Adobe Camera Raw For Digital Photographers Only by Rob Sheppard(16953)
Grails in Action by Glen Smith Peter Ledbrook(16196)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(13868)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(11874)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(10677)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10592)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(9784)
Hit Refresh by Satya Nadella(9101)
The Kubernetes Operator Framework Book by Michael Dame(8534)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8358)
Robo-Advisor with Python by Aki Ranin(8303)